# coding=utf-8
# 全世界最帅的帅哥
import hashlib
# from ysgg1_club_wx.utils import ierror
from flask import request
from ysgg1_club_wx.config.wx_config import WxConfig


def callback():
    try:
        data = request.args
        if len(data) == 0:
            return "hello, this is handle view"
        signature = data.get("signature")
        timestamp = data.get("timestamp")
        nonce = data.get("nonce")
        echostr = data.get("echostr")
        token = WxConfig.wx_token

        # 参数校验
        if not all([signature, timestamp, nonce, echostr]):
            return {"code": 400, "message": "wrong request"}

        li = [token, timestamp, nonce]
        li.sort()
        tmp_str = "".join(li).encode("utf-8")
        sign = hashlib.sha1(tmp_str).hexdigest()
        # 将自己的签名与微信的进行验证
        if sign == signature:
            return int(echostr)
        else:
            return False
    except Exception as e:
        return e


def receive_ordinary_message():
    args_data = request.args
    data = request.data

    return {"code": 200, "message": "ok"}
